<?php
/**
 * Created by PhpStorm.
 * User: zhangyi
 * Date: 17/3/5
 * Time: 下午2:25
 */

namespace backend\controllers;

use app\models\Exclusive;
use app\models\ExclusiveSearch;
use app\models\Feedback;
use app\models\Focuspic;
use app\models\Member;
use app\models\MemberSearch;
use app\models\Message;
use app\models\Notice;
use app\models\Push;
use app\models\Singlepage;
use backend\models\User;
use Yii;
use yii\web\Controller;
use yii\data\ActiveDataProvider;
use yii\captcha\Captcha;
use backend\components\Sms;


class ManageController extends Controller
{

    public function actionIndex()
    {

        $provider = new ActiveDataProvider([
             'query' => Singlepage::find(),
             'pagination' => [
                 'pageSize' => 15,
             ],
             'sort' => [
                 'defaultOrder' => [
                     'id' => SORT_DESC,
                 ]
             ],
         ]);

        return $this->render('index',[
            'models'=>$provider->getModels(),
            'pages'=>$provider->getPagination(),
        ]);
    }
    public function actionArticledit()
    {
        $id = Yii::$app->request->get('id',null);
        if($id!==null) {
            $model = Singlepage::findOne($id);
        } else {
            $model = new Singlepage();
        }

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        } else {
            return $this->render('articledit', [
                'model' => $model,
            ]);
        }
    }
    //
    public function actionNotice()
    {
        $provider = new ActiveDataProvider([
            'query' => Notice::find(),
            'pagination' => [
                'pageSize' => 15,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);

        return $this->render('notice',[
            'models'=>$provider->getModels(),
            'pages'=>$provider->getPagination(),
        ]);
    }
    public function actionNoticeEdit()
    {
        $id = Yii::$app->request->get('id',null);
        if($id!==null) {
            $model = Notice::findOne($id);
        } else {
            $model = new Notice();
        }

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['notice']);
        } else {
            return $this->render('notice-edit', [
                'model' => $model,
            ]);
        }
    }
    public function actionDelnotice()
    {
        $id = Yii::$app->request->post('id',null);
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = Notice::findOne($id);
        if($model && $model->delete()) {
            return array('f'=>1);

        } else {
            return array('data'=>'删除失败');

        }
    }
    //

    public function actionCate()
    {
        $searchModel = new ExclusiveSearch();
        $provider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('cate',[
            'models'=>$provider->getModels(),
            'pages'=>$provider->getPagination(),
            'searchModel'=>$searchModel,
        ]);
    }
    public function actionCateedit()
    {
        $id = Yii::$app->request->get('id',null);
        if($id!==null) {
            $model = Exclusive::findOne($id);
        } else {
            $model = new Exclusive();
        }

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['cate']);
        } else {
            return $this->render('cateedit', [
                'model' => $model,
            ]);
        }
    }

    public function actionDelcate()
    {
        $id = Yii::$app->request->post('id',null);
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = Exclusive::findOne($id);
        if($model && $model->delete()) {
            return array('f'=>1);

        } else {
            return array('data'=>'删除失败');

        }
    }

    public function actionUser()
    {
        $searchModel = new MemberSearch();
        $provider = $searchModel->search(Yii::$app->request->queryParams);
        $gets = array();
//        print_r(Yii::$app->request->queryParams);die;

        foreach(Yii::$app->request->queryParams as $key=>$value) {
            if(is_array($value)) {
                foreach($value as $k=>$v) {
                    $tmpkey = $key.'['.$k.']';
                    $gets[$tmpkey] = $v;
                }
            }

        }
        return $this->render('user',[
            'models'=>array_reverse($provider->getModels()),
            'pages'=>$provider->getPagination(),
            'searchModel'=>$searchModel,
            'gets'=>$gets,
        ]);
    }

    public function actionDeluser()
    {
        $id = Yii::$app->request->post('id',null);
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = Member::findOne($id);
        if($model && $model->delete()) {
            return array('f'=>1);

        } else {
            return array('data'=>'删除失败');

        }
    }

    public function actionUserinfo()
    {
        $id = Yii::$app->request->get('id',null);

        $model = Member::findOne($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['userinfo','id'=>$id]);
        } else {
            return $this->render('userinfo', [
                'model' => $model,
            ]);
        }
    }

    public function actionFankui()
    {
        $provider = new ActiveDataProvider([
            'query' => Feedback::find(),
            'pagination' => [
                'pageSize' => 15,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);

        return $this->render('fankui',[
            'models'=>$provider->getModels(),
            'pages'=>$provider->getPagination(),
        ]);
    }

    public function actionJiaodian()
    {
        $provider = new ActiveDataProvider([
            'query' => Focuspic::find(),
            'pagination' => [
                'pageSize' => 15,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);

        return $this->render('jiaodian',[
            'models'=>$provider->getModels(),
            'pages'=>$provider->getPagination(),
        ]);
    }

    public function actionDeljiaodian()
    {
        $id = Yii::$app->request->post('id',null);
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = Focuspic::findOne($id);
        if($model && $model->delete()) {
            return array('f'=>1);

        } else {
            return array('data'=>'删除失败');

        }
    }

    public function actionJiaodianedit()
    {
        $id = Yii::$app->request->get('id',null);
        if($id!==null) {
            $model = Focuspic::findOne($id);
        } else {
            $model = new Focuspic();
        }

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['jiaodian']);
        } else {
            return $this->render('jiaodianedit', [
                'model' => $model,
            ]);
        }
    }

    public function actionMyinfo()
    {
        $id = Yii::$app->user->id;
        $model = User::findOne($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['myinfo']);
        } else {
            return $this->render('myinfo', [
                'model' => $model,
            ]);
        }
    }

    public function actionExport()
    {
        $searchModel = new MemberSearch();
        $provider = $searchModel->search(Yii::$app->request->queryParams);
        $provider->setPagination(false);
        $allModels = array();
        $models = $provider->getModels();
        foreach($models as $index=>$model) {
            $model->create_time = date('Y-m-d H:i:s',$model->create_time);
            $model->order_number = $index+1;
            $allModels[$index] = $model;
        }

        return \moonland\phpexcel\Excel::widget([
            'models' => $allModels,
            'mode' => 'export', //default value as 'export'
            'columns' => ['order_number','username','mobile','create_time','zhuangtai','note','channel'], //without header working, because the header will be get label from attribute label.
            'headers' => ['order_number' => '序号','username' => '用户名', 'mobile' => '手机号','create_time' => '注册时间','zhuangtai' => '状态','note' => '备注','channel'=>'渠道'],
        ]);
    }

    public function actionEditMemberinfo()
    {
        $post = Yii::$app->request->post();
        $model = Member::findOne($post['id']);
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        if($model) {
            $type = $post['type'];
            $model->$type = $post['content'];
            if($model->save()) {
                return array(
                    'error'=>'ok',
                    'content'=>$model->$type,
                );
            } else {
                return array(
                    'error'=>'500',
                    'errorinfo'=>'内部错误,请联系管理员',
                );
            }

        } else {
            return array(
                'error'=>'500',
                'errorinfo'=>'用户不存在',
            );
        }

    }
    //导入用户
    private function importMember()
    {
//        $a = \moonland\phpexcel\Excel::import(Yii::getAlias('@webroot/member.xlsx'));
//        foreach($a as $one) {
//            $model = new Member();
//            $model->id = $one['id'];
//            $model->username = $one['username'];
//            $model->password = $model->generatePassword('*&^%$)');
//
//            $model->mobile = $one['mob'];
//            $model->status = $one['state'];
//            $model->zhuangtai = $one['status'];
//            $model->note = $one['remark'];
//            $model->create_time = strtotime($one['add_time']);
//            $model->save();
//            $model = null;
//        }
//        die;
    }

    public function actionMessage()
    {
//        phpinfo();die;
        $provider = new ActiveDataProvider([
            'query' => Message::find(),
            'pagination' => [
                'pageSize' => 15,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);

        return $this->render('message',[
            'models'=>$provider->getModels(),
            'pages'=>$provider->getPagination(),
        ]);
    }

    public function actionMessageedit()
    {
        $id = Yii::$app->request->get('id',null);
        if($id!==null) {
            $model = Message::findOne($id);
        } else {
            $model = new Message();
        }

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['message']);
        } else {
            return $this->render('messageedit', [
                'model' => $model,
            ]);
        }
    }

    public function actionDelmessage()
    {
        $id = Yii::$app->request->post('id',null);
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = Message::findOne($id);
        if($model && $model->delete()) {
            return array('f'=>1);

        } else {
            return array('data'=>'删除失败');

        }
    }

    public function actionPushmessage($id)
    {
        $pmodel = new Push();

        $model = Member::findOne($id);
        if($model) {
            $pmodel->ids = $id;
            $pmodel->targetNum = 1;

        } elseif($id==0) {
            if(in_array('all',Yii::$app->request->post('ids'))) {
                $searchModel = new MemberSearch();
                $provider = $searchModel->search(Yii::$app->request->post('condition'));
                $provider->pagination = false;
//                print_r($provider->getCount());die;
                $pmodel->ids = 'all';
                $pmodel->targetNum = $provider->getCount();
                $pmodel->condition = Yii::$app->request->post('condition') ? json_encode(Yii::$app->request->post('condition')):'';
            } else {
                $pmodel->ids = join(',',Yii::$app->request->post('ids'));

                $pmodel->targetNum = count(Yii::$app->request->post('ids'));
            }

        }

        return $this->renderAjax('pushmessage',[
            'pmodel'=>$pmodel,
        ]);
    }

    public function actionPush()
    {
        $model = new Push();
        $response = Yii::$app->response;
        $response->format = \yii\web\Response::FORMAT_JSON;
        if ($model->load(Yii::$app->request->post()) && $model->pushmessage()) {
            return ['pushstate'=>['success'=>'ok']];
        }
        return ['pushstate'=>['success'=>'fail']];

    }

}
